package ningbo.ticket;

/**
 * @Author 宁铂
 * @Date 2021/7/28 20:47
 * @Version 1.0
 */
public class TestRunnable {
    /** 通过Runnable接口的方法实现多线程售票案例
     *  要求: 4个窗口 总共100张票,售完为止
     * */
    public static void main(String[] args) {

        TicketR ticketR = new TicketR();

        Thread thread1 = new Thread(ticketR,"窗口一");
        Thread thread2 = new Thread(ticketR,"窗口二");
        Thread thread3 = new Thread(ticketR,"窗口三");
        Thread thread4 = new Thread(ticketR,"窗口四");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}

class TicketR implements Runnable{

    int tickets = 100;
    Object o = new Object();

    @Override
    public void run() {
        while(true){
            synchronized (o){
                if (tickets > 0) {
                    System.out.println(Thread.currentThread() + "=" + tickets--);
                }
                if (tickets <= 0) break;
            }
        }
    }
}
